package cn.lcj.org.service.impl;

import cn.lcj.basic.service.impl.BaseServiceImpl;
import cn.lcj.basic.utils.Md5Utils;
import cn.lcj.basic.utils.StrUtils;
import cn.lcj.org.domain.Department;
import cn.lcj.org.domain.Employee;
import cn.lcj.org.mapper.DepartmentMapper;
import cn.lcj.org.mapper.EmployeeMapper;
import cn.lcj.org.service.IEmployeeService;
import cn.lcj.user.domain.Logininfo;
import cn.lcj.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * $
 *
 * @return $
 * @date $ $
 * @description $
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl <Employee> implements IEmployeeService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public List<Department> departmentTree() {
        //查询全部将department对象放进一个数组里面
        List<Department> departments = departmentMapper.loadAll();
        //1.将所有部门信息保存在map中，方便根据id去获取部门信息，就不用查数据库了
        Map<Long, Department> map = new HashMap<>();
        for (Department department : departments) {
            //将数据放入map内存里面
            map.put(department.getId(), department);
        }
        //2.遍历所有的部门，如果是顶级部门[parent_id==null],加到部门树
        List<Department> deptTree = new ArrayList<>();
        for (Department department : departments) {
            if (department.getParent_id() == null) {
                deptTree.add(department);
                //3.如果不是顶级部门[parent_id!=null],找到自己的父部门，将自己加到父部门的children
            } else {
                //获取父部门的id - 获取父部门对象 - 获取父部门的children集合
                Long parentId = department.getParent_id();
                //根据id获取父部门
                Department parentDept = map.get(parentId);
                //防止数据表代码不对应空指针异常
                if (parentDept != null) {
                    parentDept.getChildren().add(department);
                }

            }
        }
        return deptTree;
    }

    @Override
    public List<Employee> queryByShopId(Long shopId) {
        return employeeMapper.queryByShopId(shopId);
    }

    @Transactional
    @Override
    public void save(Employee employee) {
        //先保存logininfo在保存employee最后保存role
        //1.处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
       String md5Pwd= Md5Utils.encrypByMd5(employee.getPassword()+salt);
       employee.setSalt(salt);
       employee.setPassword(md5Pwd);
        Logininfo logininfo =employee2logininfo(employee);
        //获取到logininfo的自增长id
        logininfoMapper.save(logininfo);
        //employee设置logininfo_id
        employee.setLogininfo_id(logininfo.getId());
        //保存employee
        super.save(employee);
        if(employee.getShop_id()!=null){
            employeeMapper.addRole(employee);
        }
    }

    private Logininfo employee2logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        return logininfo;


    }

    @Transactional
    @Override
    public void update(Employee employee) {
        //1.处理盐值和密码
        Employee dbEmployee = employeeMapper.loadById(employee.getId());
        //数据库的密码与前段传来的相等，说明盐值和密码与数据库一致
        if (employee.getPassword().equals(dbEmployee.getPassword())){
            String salt=dbEmployee.getSalt();
            String password= dbEmployee.getPassword();
            employee.setSalt(salt);
            employee.setPassword(password);
        }else{
            String salt = StrUtils.getComplexRandomString(32);
            String md5Pwd= Md5Utils.encrypByMd5(employee.getPassword()+salt);
            employee.setSalt(salt);
            employee.setPassword(md5Pwd);
        }

        Logininfo logininfo =employee2logininfo(employee);
        //logininfo_id被覆盖掉了，所以需要通过老的employee里面的数据去获取
        //重值logininfo的id
        logininfo.setId(dbEmployee.getLogininfo_id());
        //设置disable  数据库没变只用重数据库从新获取
        Logininfo dbLogininfo = logininfoMapper.loadById(logininfo.getId());
        logininfo.setType(dbLogininfo.getType());
        logininfo.setDisable(dbLogininfo.getDisable());
        logininfoMapper.update(logininfo);



        //修改employee的信息
        employee.setShop_id(dbEmployee.getShop_id());
        //修改employee
        super.update(employee);
        //修改role 先删除在修改
        employeeMapper.delRoleByEmployeeId(employee.getId());
        if(employee.getShop_id()!=null){
            employeeMapper.addRole(employee);
        }
    }
    @Transactional
    @Override
    public void del(Long id) {
        //需要先删除logininfo再删除employee最后删除t_employee_role否则会报外键约束的错误
        Employee employee = employeeMapper.loadById(id);
        //删除logininfo
        logininfoMapper.del(employee.getLogininfo_id());

        employeeMapper.delRoleByEmployeeId(id);
        super.del(id);

    }
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            super.del(id);
        }

    }
}
